﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

using BOL.IO;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.IO
{
    public class GRBWriter : IWriter
    {
        #region Private variables

        private Network _network;
        private FileStream _fs;

        #endregion

        #region Constructors

        public GRBWriter(Network network, FileStream fs)
        {
            _network = network;
            _fs = fs;
        }

        public GRBWriter(Network network, string path)
            : this(network, new FileStream(path, FileMode.Create)) { }

        #endregion

        #region IWriter implementation

        public bool Write()
        {
            using (var bw = new BinaryWriter(_fs))
            {
                bw.Write(_network.Name);
                bw.Write(_network.Description);

                foreach (var edge in _network.Edges)
                {
                    bw.Write(edge.Source.Id);
                    bw.Write(edge.Target.Id);
                    bw.Write((uint)edge.RelationTypes);
                }

                return true;
            }
        }

        public void Close()
        {
            _fs.Close();
        }

        #endregion
    }
}
